﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using GZ.Entity;
using System.Data;

public partial class order : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txt_DeliveryDate.Value = DateTime.Now.ToString("yyyy-MM-dd");

            if (!String.IsNullOrEmpty(Request.QueryString["t"]))
            {
                LoadCart();
            }
        }
    }
    /// <summary>
    /// 当点击快速购物后快速构建购物车并将产品展示给客户
    /// </summary>
    public void LoadCart()
    {
        HttpCookie cookie = null;
        StringBuilder strs = null;

        EProductInfo proAccessor = new EProductInfo();
        ECategory cateAccessor = new ECategory();
        proAccessor.IsShelves = 1;


        System.Collections.Generic.List<EProductInfo> products = proAccessor.GetProductInfos(proAccessor);
        DataTable totalCates = cateAccessor.GetCategory();

        if (products != null && products.Count != 0 && totalCates != null && totalCates.Rows.Count != 0)
        {
            #region 拼装指定格式的购物车产品数据并存入购物车cookie对象中

            foreach (EProductInfo product in products)
            {
                intoCart(product);
            }

            #endregion

            #region 拼装指定格式的购物车分类数据并存入购物车cookie对象中

            cookie = new HttpCookie("totalCates");
            cookie.Expires = DateTime.Now.AddDays(1);
            strs = new StringBuilder();
            foreach (DataRow cate in totalCates.Rows)
            {
                strs.AppendFormat("{0}_", cate["Id"]);
            }
            cookie.Value = strs.ToString().TrimEnd(new char[] { '_' });
            Response.Cookies.Add(cookie);

            #endregion
        }
    }

    /// <summary>
    /// 添加产品进入购物车
    /// </summary>
    /// <param name="product"></param>
    public void intoCart(EProductInfo product)
    {
        bool isExt = false;
        String cartsObj = string.Empty;
        HttpCookie cookie;
        if (Request.Cookies["carList"] != null)
        {
            cookie = Request.Cookies["carList"];
            cartsObj = cookie.Value;
            if (cartsObj.IndexOf("&") > -1)
            {
                String[] carts = cartsObj.Split(new char[] { '&' });
                foreach (String cart in carts)
                {
                    if (cart.Split(new char[] { '=' })[0] == product.Id.ToString())
                    {
                        isExt = true;
                        break;
                    }
                }
            }
            else
            {
                if (cartsObj.Split(new char[] { '=' })[0] == product.Id.ToString())
                {
                    isExt = true;
                }
            }
            //不存在购物车中
            if (!isExt)
            {
                cartsObj += string.Format("&{0}={0}|{1}|{2}|{3}", product.Id, 0, product.Price, product.CategoryId);
                cookie.Value = cartsObj;
                Response.Cookies.Add(cookie);
            }
            else
            {

            }
        }
        else
        {
            cookie = new HttpCookie("carList");
            cookie.Expires = DateTime.Now.AddDays(1);
            cookie.Value = string.Format("{0}={0}|{1}|{2}|{3}", product.Id, 0, product.Price, product.CategoryId);
            Response.Cookies.Add(cookie);
        }

    }

    /// <summary>
    /// 获取单品点击后的数量，防止点击立即购买后的购物车默认数量值覆盖其值
    /// </summary>
    /// <param name="pid"></param>
    /// <returns></returns>
    private Int32 getProCnt(String pid)
    {
        int cnt = 0;
        if (Request.Cookies["carList"] != null)
        {
            String cartList = Request.Cookies["carList"].Value;

            if (cartList.IndexOf("&") > -1)
            {
                String[] cartItems = cartList.Split(new char[] { '&' });
                Int32 index = getIndexFromCart(cartList, pid);

                cnt = Convert.ToInt32(cartItems[index].Split(new char[] { '|' })[1]);
            }
            else
            {
                cnt = Convert.ToInt32(cartList.Split(new char[] { '|' })[1]);
            }
        }

        return cnt;
    }

    /// <summary>
    /// 获取产品对应数量的位置所在索引
    /// </summary>
    /// <param name="cartList"></param>
    /// <param name="pid"></param>
    /// <returns></returns>
    private int getIndexFromCart(String cartList, String pid)
    {
        String[] cartItems = cartList.Split(new char[] { '&' });

        int index = 0;
        for (int i = 0; i < cartItems.Length; i++)
        {
            if (cartItems[i].Split(new Char[] { '=' })[0] == pid)
            {
                index = i;
                break;
            }
        }
        return index;
    }
}